package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getEmployees")
public class GetEmployeesServlet extends HttpServlet {

    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();
        
        System.out.println("GetEmployeesServlet: 开始处理请求");
        
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.print(new Gson().toJson(result));
            System.out.println("GetEmployeesServlet: 用户未登录");
            return;
        }
        
        User currentUser = (User) session.getAttribute("user");
        String companyId = currentUser.getCompanyId();
        
        System.out.println("GetEmployeesServlet: 当前用户ID=" + currentUser.getId() + ", 角色=" + currentUser.getRole() + ", 公司ID=" + companyId);
        
        if (companyId == null || companyId.isEmpty()) {
            result.put("success", false);
            result.put("message", "您尚未加入公司");
            out.print(new Gson().toJson(result));
            System.out.println("GetEmployeesServlet: 用户未加入公司");
            return;
        }
        
        // 获取搜索关键词
        String keyword = request.getParameter("keyword");
        System.out.println("GetEmployeesServlet: 搜索关键词=" + keyword);
        
        try {
            // 获取员工列表
            List<CompanyEmployee> employees;
            if (keyword != null && !keyword.trim().isEmpty()) {
                employees = companyEmployeeService.searchEmployees(companyId, keyword);
                System.out.println("GetEmployeesServlet: 搜索员工, 关键词=" + keyword + ", 结果数量=" + (employees != null ? employees.size() : 0));
            } else {
                employees = companyEmployeeService.getEmployeesByCompanyId(companyId);
                System.out.println("GetEmployeesServlet: 获取所有员工, 结果数量=" + (employees != null ? employees.size() : 0));
            }
            
            result.put("success", true);
            result.put("employees", employees);
            result.put("isManager", currentUser.getRole() == 1); // 判断是否是公司负责人
            
            // 打印返回的JSON数据大小
            String jsonResult = new Gson().toJson(result);
            System.out.println("GetEmployeesServlet: 返回JSON数据大小=" + jsonResult.length() + "字节");
            
            out.print(jsonResult);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "获取员工列表失败: " + e.getMessage());
            out.print(new Gson().toJson(result));
            System.out.println("GetEmployeesServlet: 发生异常: " + e.getMessage());
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
} 